﻿using NFine.Code;
using NFine.Domain._03_Entity.Weixin;
using NFine.Domain._04_IRepository.Weixin;
using NFine.Repository.Weixin;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace NFine.Application._02_Weixin
{
    public class WechatInfoApp
    {
        private IWechatInfoRepository service = new WechatInfoRepository();

        private Log logger = LogFactory.GetLogger("NFine.Application.Biz.WechatInfoApp");

        /// <summary>
        /// 根据unionid查询对象
        /// </summary>
        /// <param name="unionid"></param>
        /// <returns></returns>
        public WechatInfoEntity GetForm(string unionid)
        {
            return service.FindEntity(a => a.F_unionid == unionid);
        }

        public WechatInfoEntity GetByOpenId(string openid)
        {
            List<WechatInfoEntity> ls = service.FindList("select * from biz_wechat_info where F_openid='" + openid + "' limit 1");
            if (ls.Count > 0)
            {
                return ls.First();
            }
            else
            {
                return null;
            }
        }

        public int SubmitForm(WechatInfoEntity entity, int keyValue = 0)
        {
            if (keyValue != 0)
            {
                entity.F_id = keyValue;
                return service.Update(entity);
            }
            else
            {
                return service.Insert(entity);
            }
        }

        public async Task<int> InsertAsync(WechatInfoEntity entity)
        {
            return await service.InsertAsync(entity);

        }

    }
}
